package com.yitiao.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.yitiao.annotation.RateLimiter;
import com.yitiao.consumer.MessageHandler;
import com.yitiao.enums.LimitType;
import com.yitiao.mq.EpcMessageCallback;
import com.yitiao.producer.RabbitMQSender;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Slf4j
public class TestController {
    @GetMapping("/limit")
    @RateLimiter(time = 10,limitType = LimitType.IP,count = 2)
    public String limitTest(){
        return "limit test";
    }

    private final RabbitMQSender mqSender;

    private final EpcMessageCallback epcCallback;

    @PostMapping("/send")
    public String sendMsg(){
        mqSender.send("exchange-1","yitiao","test1");
        mqSender.send("exchange-1","yitiao","test2",epcCallback);
        mqSender.send("exchange-1","yitiao","test3",new CorrelationData("test333"));
        mqSender.send("exchange-1","yitiao","test3",new CorrelationData("test333"),epcCallback);

        return "success";
    }

    @GetMapping("/handler")
    public String handler(){
        Map<String, MessageHandler> messageHandlerMap = SpringUtil.getBean("messageHandlerMap");
        messageHandlerMap.forEach((k, v)->{
            System.out.println(k+" "+v);
        });
        return "success";
    }
}
